package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

/**
 * 管理员登录
 *
 * @author Mingyue
 *
 */
@WebServlet("/admin/isbn_search")
public class ISBNsearch  extends HttpServlet {

//    public static  void main(String aug[])
//    {
//        String apikey="12469.fcd2ac4e9b8099d51e8db251737d84b8.ed524086f8e7ec7c9d0aaa5c9c3dd350";
//        String isbn = "9787020024759";
//        isbn_inf infa = new isbn_inf(isbn,apikey);
//        System.out.println(infa.name);
//    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置头文件
        response.setContentType("application/json; charset=utf8");
        PrintWriter out = response.getWriter();
        // 获取账号密码
//        String username = request.getParameter("username");
        String isbn = request.getParameter("ISBN");
//        String password = request.getParameter("password");
        String apikey="12469.fcd2ac4e9b8099d51e8db251737d84b8.ed524086f8e7ec7c9d0aaa5c9c3dd350";
        isbn_inf infa = new isbn_inf(isbn,apikey);
        String a =null;


        // 设置响应map
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        if (infa!=null) {
            HttpSession session = request.getSession();
            session.setAttribute("bookname", infa.name);
            session.setAttribute("author", infa.author);
            session.setAttribute("description",infa.description);
            hashMap.put("code", 0);
            hashMap.put("msg", "添加成功");
            hashMap.put("url", request.getContextPath() + "/bookaddisbn.jsp");
//            hashMap.put("url", request.getContextPath() +"/admin/index.jsp");
        }else {
            hashMap.put("code", 1);
            hashMap.put("msg", "请重新添加");
        }

        JSONObject json = JSONObject.fromObject(hashMap);
        out.write(json.toString());

    }

}



